//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.neurosky.thinkgear;

import android.os.Environment;
import android.util.Log;
import com.neurosky.thinkgear.EnergyLevel;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class NeuroSkyHeartMeters {
    public static int initRindex = 5;
    public static boolean verbose = false;
    public static boolean agelimits = true;
    private static String a;
    private static Integer[] b;
    private static Integer[] c;

    public NeuroSkyHeartMeters() {
    }

    public static double calculateHeartAge(Integer[] var0, int var1, int var2) {
        long var3 = System.nanoTime();
        b = new Integer[var0.length];

        int var5;
        for(var5 = 0; var5 < var0.length; ++var5) {
            b[var5] = Integer.valueOf(var0[var5].intValue());
        }

        for(var5 = 7; var5 < var0.length - 1; ++var5) {
            c = new Integer[]{var0[var5 - 7], var0[var5 - 6], var0[var5 - 5], var0[var5 - 4], var0[var5 - 3], var0[var5 - 2], var0[var5 - 1]};
            if((double)var0[var5].intValue() > 1.5D * (double)a(c) || (double)var0[var5].intValue() < 0.6D * (double)a(c)) {
                b[var5] = Integer.valueOf((int)((double)((float)var0[var5 + 1].intValue() + (float)var0[var5 - 1].intValue()) / 2.0D));
            }
        }

        var0 = new Integer[b.length - 8];

        for(var5 = 7; var5 < b.length - 1; ++var5) {
            var0[var5 - 7] = b[var5];
        }

        double var11 = calculateHeartAgeRaw(var0);
        double var7 = (double)var1 - var11;
        long var9 = System.nanoTime();
        if(verbose) {
            System.out.println("Heart age: time in nanoseconds: " + (var9 - var3));
        }

        return (double)var1 - var7 / (double)var2;
    }

    private static int a(Integer[] var0) {
        Arrays.sort(var0);
        float var1 = (float)((double)((float)var0.length) / 2.0D);
        return var0.length % 2 == 0?(int)((double)(var0[(int)var1 - 1].intValue() + var0[(int)var1].intValue()) / 2.0D):var0[(int)Math.floor((double)var1)].intValue();
    }

    public static void resetHeartAge(String var0) {
        if("mounted".equals(Environment.getExternalStorageState())) {
            a = Environment.getExternalStorageDirectory() + "/Android/data/com.neurosky.thinkgear/files/EKG/age";
        } else {
            a = Environment.getDataDirectory() + "/com.neurosky.thinkgear/files/EKG/age";
        }

        File var1 = new File(a);
        File var3 = new File(a, var0 + ".json");

        try {
            if(!var1.exists()) {
                var1.mkdirs();
            }

            if(var3.exists()) {
                var3.delete();
                var3.createNewFile();
                return;
            }

            var3.createNewFile();
        } catch (IOException var2) {
            ;
        }

    }

    public static int progressHeartAge(String var0) {
        File var6 = a(var0);
        int var1 = 0;
        if(var6 == null) {
            return -1;
        } else {
            try {
                BufferedReader var2 = new BufferedReader(new FileReader(var6));
                var0 = "";

                do {
                    var0 = var0 + var2.readLine();
                } while(var2.ready());

                var2.close();
                var1 = ((JSONObject)(new JSONTokener(var0)).nextValue()).getJSONArray("rrIntervalInMS").length();
            } catch (FileNotFoundException var3) {
                var3.printStackTrace();
            } catch (IOException var4) {
                ;
            } catch (JSONException var5) {
                ;
            }

            return var1;
        }
    }

    public static double calculateHeartAge(Integer[] var0, int var1, String var2) {
        System.nanoTime();
        if(a(var2) == null) {
            resetHeartAge(var2);
        }

        if(verbose) {
            System.out.println("Values for byte array");
        }

        byte[] var3 = b(var2);

        for(int var5 = 0; var5 < var3.length; ++var5) {
            if(verbose) {
                System.out.println("Value " + Integer.toString(var5) + ":" + Integer.toString(var3[var5]));
            }
        }

        double var15 = calculateHeartAgeRaw(var0);
        byte[] var14 = new byte[var3.length + 1];

        int var8;
        for(var8 = 0; var8 < var3.length; ++var8) {
            var14[var8] = var3[var8];
        }

        var14[var14.length - 1] = (byte)((int)var15);
        a(var2, var14);
        if(verbose) {
            System.out.println("Values for byte array2");
        }

        for(var8 = 0; var8 < var14.length; ++var8) {
            if(verbose) {
                System.out.println("Value " + Integer.toString(var8) + ":" + Integer.toString(var14[var8]));
            }
        }

        double var16 = 0.0D;

        for(int var10 = 0; var10 < var14.length; ++var10) {
            var16 += (double)var14[var10];
        }

        var16 /= (double)var14.length;
        double var17 = (double)var1 - var16;
        double var12;
        if((var12 = (double)(8 - var14.length)) < 1.0D) {
            var12 = 1.0D;
        }

        return (double)var1 - var17 / var12;
    }

    public static double calculateHeartAgeRaw(Integer[] var0) {
        long var1 = System.nanoTime();
        double var3 = calculateSDNN(var0);
        double var5 = (85.0D - var3) / 0.57D;
        long var7 = System.nanoTime();
        if(verbose) {
            System.out.println("Heart age uncorected: time in nanoseconds: " + (var7 - var1));
        }

        if(agelimits) {
            if(var5 > 99.0D) {
                var5 = 99.0D;
            }

            if(var5 < 10.0D) {
                var5 = 10.0D;
            }
        }

        return var5;
    }

    public static int calculateHeartRiskAware(String var0) {
        byte[] var5;
        if((var5 = b(var0)).length == 0) {
            return -1;
        } else {
            double[] var1 = new double[var5.length];
            double var3 = 0.0D;

            for(int var2 = initRindex; var2 < var5.length; ++var2) {
                var1[var2] = 85.0D - 0.57D * (double)var5[var2];
                var3 += var1[var2] / (double)var5.length;
            }

            byte var6;
            if(var3 >= 30.0D) {
                var6 = 0;
            } else if(var3 >= 20.0D) {
                var6 = 1;
            } else if(var3 >= 10.0D) {
                var6 = 2;
            } else {
                var6 = 3;
            }

            return var6;
        }
    }

    public static int calculateHeartRiskAware(Integer[] var0) {
        long var1 = System.nanoTime();
        double var3;
        byte var8;
        if((var3 = calculateSDNN(var0)) >= 30.0D) {
            var8 = 0;
        } else if(var3 >= 20.0D) {
            var8 = 1;
        } else if(var3 >= 10.0D) {
            var8 = 2;
        } else {
            var8 = 3;
        }

        long var6 = System.nanoTime();
        if(verbose) {
            System.out.println("Heart risk: time in nanoseconds: " + (var6 - var1));
        }

        return var8;
    }

    public static double calculateSDNN(Integer[] var0) {
        long var1 = System.nanoTime();
        double var3 = 0.0D;

        for(int var5 = initRindex; var5 < var0.length; ++var5) {
            var3 += (double)var0[var5].intValue();
        }

        double var15 = var3 / (double)(var0.length - initRindex);
        double var7 = 0.0D;

        for(int var9 = initRindex; var9 < var0.length; ++var9) {
            var7 += ((double)var0[var9].intValue() - var15) * ((double)var0[var9].intValue() - var15);
        }

        double var11 = Math.sqrt(var7 / (double)(var0.length - initRindex - 1));
        long var13 = System.nanoTime();
        if(verbose) {
            System.out.println("SDNN: time in nanoseconds: " + (var13 - var1));
        }

        return var11;
    }

    public static int calculateRelaxationLevel(int[] var0) {
        long var1 = System.nanoTime();
        int var7 = (new EnergyLevel()).calculateEnergyLevel(var0, var0.length);
        long var5 = System.nanoTime();
        if(verbose) {
            System.out.println("Energy level: time in nanoseconds: " + (var5 - var1));
        }

        return var7;
    }

    public static double calculateMult() {
        long var0 = System.nanoTime();
        double var2 = 3.141592653589793D;

        for(int var4 = 1; var4 < 100000; ++var4) {
            var2 *= 3.141592653589793D;
        }

        long var6 = System.nanoTime();
        if(verbose) {
            System.out.println("Mult: time in nanoseconds: " + (var6 - var0));
        }

        return var2;
    }

    public static double calculateMult3() {
        return 3.141592653589793D;
    }

    public static double calculateMult2() {
        long var0 = System.nanoTime();
        long var2 = System.nanoTime();
        if(verbose) {
            System.out.println("Mult: time in nanoseconds: " + (var2 - var0));
        }

        return 1.0D / 0.0;
    }

    private static File a(String var0) {
        if("mounted".equals(Environment.getExternalStorageState())) {
            a = Environment.getExternalStorageDirectory() + "/Android/data/com.neurosky.thinkgear/files/EKG/age";
        } else {
            a = Environment.getDataDirectory() + "/com.neurosky.thinkgear/files/EKG/age";
        }

        File var1 = new File(a);
        File var3 = new File(a, var0 + ".json");

        try {
            if(!var1.exists()) {
                var1.mkdirs();
            }

            if(!var3.exists()) {
                var3.createNewFile();
            }

            return var3;
        } catch (IOException var2) {
            return null;
        }
    }

    private static boolean a(String var0, byte[] var1) {
        File var4;
        if((var4 = a(var0)) == null) {
            return false;
        } else {
            String var2 = null;

            try {
                FileOutputStream var5 = new FileOutputStream(var4, false);
                var2 = a(var1);
                var5.write(var2.getBytes());
                var5.close();
                return true;
            } catch (IOException var3) {
                var3.printStackTrace();
                return false;
            }
        }
    }

    private static byte[] b(String var0) {
        String var1 = "";
        File var5 = a(var0);
        byte[] var2 = new byte[]{(byte)0};
        if(var5 == null) {
            return var2;
        } else {
            try {
                BufferedReader var6 = new BufferedReader(new FileReader(var5));

                do {
                    var1 = var1 + var6.readLine();
                } while(var6.ready());
            } catch (FileNotFoundException var3) {
                var3.printStackTrace();
            } catch (IOException var4) {
                ;
            }

            return c(var1);
        }
    }

    private static byte[] c(String var0) {
        byte[] var1 = new byte[1];

        try {
            JSONArray var5;
            var1 = new byte[(var5 = ((JSONObject)(new JSONTokener(var0)).nextValue()).getJSONArray("templateArray")).length()];

            for(int var2 = 0; var2 < var5.length(); ++var2) {
                var1[var2] = (byte)var5.getInt(var2);
            }
        } catch (JSONException var3) {
            ;
        } catch (ClassCastException var4) {
            ;
        }

        return var1;
    }

    private static String a(byte[] var0) {
        JSONObject var1 = new JSONObject();
        JSONArray var2 = new JSONArray();
        Log.v("TGDevice", "toJSONString() template array: " + var0.length);

        for(int var3 = 0; var3 < var0.length; ++var3) {
            var2.put(var0[var3]);
        }

        Log.v("TGDevice", "toJsonString() arraylength:" + var2.length());

        try {
            var1.accumulate("rrIntervalInMS", var2);
        } catch (JSONException var4) {
            var4.printStackTrace();
        }

        return var1.toString();
    }
}
